package com.magold.game;

import org.andengine.engine.camera.SmoothCamera;
import org.andengine.opengl.util.GLState;

public class MagoldCamera extends SmoothCamera {

	public MagoldCamera(float pX, float pY, float pWidth, float pHeight, float pMaxVelocityX, float pMaxVelocityY, float pMaxZoomFactorChange) {
		super(pX, pY, pWidth, pHeight, pMaxVelocityX, pMaxVelocityY, pMaxZoomFactorChange);
	}

	@Override
	protected void ensureInBounds() {
		final float centerX;
		if(this.mBoundsWidth < this.getWidth()) {
			centerX = this.mBoundsCenterX;
		} else {
			float realXMin = getXMin();
			float realXMax = getXMax();
			if (realXMin < mBoundsXMin) {
				centerX = mBoundsXMin + getWidth() / 2;
			} else if (realXMax > mBoundsXMax){
				centerX = mBoundsXMax - getWidth() / 2;
			} else {
				centerX = (realXMax+realXMin) / 2;
			}
		}
		
		final float centerY;
		if(this.mBoundsHeight < this.getHeight()) {
			centerY = this.mBoundsCenterY;
		} else {
			float yMin = getYMin();
			float yMax = getYMax();
			if (yMin < mBoundsYMin) {
				centerY = mBoundsYMin + getHeight() / 2;
			} else if (yMax > mBoundsYMax){
				centerY = mBoundsYMax - getHeight() / 2;
			} else {
				centerY = (yMax+yMin) / 2;
			}
		}
		final float dX = centerX - this.getCenterX();
		final float dY = centerY - this.getCenterY();

		this.mXMin += dX;
		this.mXMax += dX;
		this.mYMin += dY;
		this.mYMax += dY;
	}
	
	@Override
	public void onApplySceneBackgroundMatrix(final GLState pGLState) {
		super.onApplySceneMatrix(pGLState);
	}
	
}
